package es.upm.gib.mappingAPI2.RDF2NL;

import java.util.ArrayList;
import java.util.List;

/**
 * This class represents a text that includes text formatting elements that indicate if an element is an RDF class, a property, plain text...
 * <p/>
 * One object of this class represents one single sentence. It is composed of an ordered list of annotated text elements
 * <p/>
 * The annotation includes which type of element it is (Format) and, in case it is a class or relation, its position in the RDF path that
 * originated it (relations take the position of their subsequent class, eg: 0-1-1-2-2-3-3)
 */
public class NLText {

    public enum Format {
        PLAIN,
        CLASS,
        RELATION,
        RESTRICTION_OPERATOR,
        RESTRICTION_OPERAND_NUMBER,
        RESTRICTION_OPERAND_TEXT
    }

    public static class AnnotatedText {

        /**
         * Natural language text of this text and format element
         */
        public String text;

        /**
         * Annotation for this text
         */
        public final Format format;

        /**
         * If the element corresponds to a class or relation element from a path, this is its position in the path (>=0), or null for others
         */
        public final Integer positionInPath;

        public AnnotatedText(String text, Format format) {
            this(text, format, null);
        }

        public AnnotatedText(String text, Format format, Integer positionInPath) {
            this.text = text;
            this.format = format;
            this.positionInPath = positionInPath;
        }

        @Override
        public String toString() {
            String str = positionInPath != null ? "[" + positionInPath + "]" : "";
            str += "{" + format.toString() + "}" + getTextString();
            return str;
        }

        public String getTextString() {
            if (format == NLText.Format.RESTRICTION_OPERAND_TEXT) {
                return "'" + text + "'";
            } else {
                return text;
            }
        }

        public void setTextString(String text) {
            this.text = text;
        }

    }

    /**
     * The list of annotated text elements
     */
    private final List<AnnotatedText> nlText;

    /**
     * Class constructor
     */
    public NLText() {
        nlText = new ArrayList<AnnotatedText>();
    }

    /**
     * Adds a new annotated text element to the end of the list of elements
     *
     * @param annotatedText new annotated text element to add
     */
    public void addTextElement(AnnotatedText annotatedText) {
        nlText.add(annotatedText);
    }

    /**
     * Retrieves the current list of annotated text elements
     *
     * @return the list of added annotated text elements
     */
    public List<AnnotatedText> getNlText() {
        return nlText;
    }

    @Override
    public String toString() {
        String str = "";
        for (int i = 0; i < nlText.size() - 1; i++) {
            str += nlText.get(i).toString() + "/";
        }
        if (!nlText.isEmpty()) {
            str += nlText.get(nlText.size() - 1).toString();
        }
        return str;
    }

    /**
     * Writes this NLText object as a single plain text (eliminating all annotation data) sentence
     *
     * @return a String with the plain text representation of this NLText object
     */
    public String toPlainText() {
        String str = "";
        for (AnnotatedText aNlText : nlText) {
            str += aNlText.getTextString();
        }
        return str;
    }
}
